<?php

namespace Frontend\Controllers;

use Models\Act;
use Models\ActUser;
use Models\Wx;


class ActController extends BaseController
{

    public function indexAction()
    {
        $token = $this->request->get('token');
        $it = Wx::findFirstByToken($token);
        $this->view->it = $it;



        $acts = Act::findByToken($token);

        if (!$acts) return $this->flash_->warning('你访问的活动页面不存在');
        $actsArr = $acts->toArray();
        for ($i = 0; $i < count($actsArr); ++$i) {
            $count = ActUser::count("aid=" . $actsArr[$i]['aid']);
            $actsArr[$i]['count'] = $count;
        }
        $currentPage = $this->request->getQuery('page');
        $paginator = new \Phalcon\Paginator\Adapter\NativeArray(array(
            "data" => $actsArr,
            "limit" => 10,
            "page" => $currentPage,
        ));
        $this->view->page = $paginator->getPaginate();
    }


    public function detailAction()
    {
        $token = $this->request->get('token');
        $aid = $this->request->get('id');
        $host = $this->config->host;
        $acturl = $host . '/frontend/act/detail' . '?id=' . $aid . '&token=' . $token;
        $this->view->setVar('acturl', $acturl);


        $act = Act::findFirstByAid($aid);
        // if(!$act) return $this->flash_->warning('你访问的活动页面不存在');
        $this->view->setVar('act', $act);
        $wx = Wx::findFirstByToken($act->token);
        $this->view->setVar('wx', $wx);

        if ($this->request->isPost() == true) {

            $name = $this->request->getPost("name");
            $tel = $this->request->getPost("tel");

            if (!$name || !$tel) {
                return $this->flash->warning("请输入完整");
            } else {
                $t = ActUser::findFirst("tel='".$tel."' AND aid ='".$aid."'");
                if (!$t) {
                    $actuser = new ActUser;
                    $actuser->name = $name;
                    $actuser->tel = $tel;
                    $actuser->time = date("Y-m-d H:i:s", strtotime("now"));
                    $actuser->aid = $aid;
                    $actuser->token = $token;
                    if ($actuser->create()) {
                        return $this->flash->warning("报名成功");
                    } else {
                        foreach ($actuser->getMessages() as $message) {
                            $this->flash->error((string)$message);
                        }
                    }
                } else {
                    return $this->flash->warning("该号码已经提交，请勿重复报名");
                }
            }

            //  return $this->response->redirect($acturl);
//            else {
//
//                if ($tel == $user->tel) {
//                    return $this->flash->warning("您已报名，请勿重复");
//                    //   return $this->response->redirect($acturl);
//                } else {
//
//                    $user->name = $name;
//                    $user->tel = $tel;
//                    $user->time = date("Y-m-d H:i:s", strtotime("now"));
//                    $user->save();
//                    return $this->flash->warning("报名成功");
//                    //  return $this->response->redirect("fronten");
//                }
//            }

        }
    }


}